<template>

  <!--
  产品分类模块
  采用后端搜索和分页
   -->
  <div class="gx-panel">

    <!-- 新增和修改弹层 -->
    <el-dialog
    :close-on-click-modal="false"
    :title="(action==='add'?'新增':'修改')+'类别'"
    :visible.sync="visible"
    width="430px"
    @closed="$refs.form.clearValidate()"
    >

      <!-- 表单 -->
      <el-form label-width="90px" :model="form" :rules="rules" ref="form">
        <el-form-item label="类别名称" prop="title">
          <el-input v-model="form.title" /> 
        </el-form-item>
        <el-form-item label="上传图片" prop="thumb">
          <el-input v-model="form.thumb">
            <el-upload
            action="http://cloud.scnew.com.cn/api/user/upload"
            slot="append"
            :show-file-list="false"
            :on-success="uploadSuccess"
            :before-upload="beforeUpload"
            >
              <el-button icon="el-icon-upload2"/>
            </el-upload>
          </el-input>
        </el-form-item>
        <el-form-item label="排序数字">
          <el-input-number v-model="form.orderid" /> 
        </el-form-item>
        <el-form-item label="审核">
          <el-switch 
          v-model="form.state" 
          :active-value="2" 
          :inactive-value="1"
          /> 
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>

    </el-dialog>

    <div class="heading">
      <h1>产品分类管理</h1>
      <el-breadcrumb>
        <el-breadcrumb-item to="/control">后台首页</el-breadcrumb-item>
        <el-breadcrumb-item to="/control/productcate">产品分类管理</el-breadcrumb-item>
        <el-breadcrumb-item>产品分类列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="search">
  
      <el-form :inline="true">

        <el-form-item v-if="ids.length">
          <el-button type="danger" size="small" @click="handleRemove(ids)">批量删除</el-button>
        </el-form-item>
        
        <el-form-item>
          <el-select 
          v-model="condition.state" 
          placeholder="选择审核"
          clearable
          size="small"
          style="width:120px"
          >
            <el-option label="未审核" :value="1" />
            <el-option label="已审核" :value="2" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-input 
          v-model.trim="condition.keyword" 
          placeholder="请输入类别名称！"
          size="small"
          clearable
          />
        </el-form-item>

      </el-form>

      <el-button 
      type="primary" 
      icon="el-icon-plus" 
      size="small"
      @click="add"
      >新增</el-button>

    </div>

    <div class="datagrid">

      <el-table
      :data="listInfo.result"
      style="width:100%"
      @selection-change="handleSelectionChange"
      ref="table"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column label="排序" prop="orderid" :width="80" sortable />
        <el-table-column label="分类图片" v-slot="scope">
          <el-image 
          style="height:60px" 
          :src="scope.row.thumb"
          fit="cover"
          />
        </el-table-column>
        <el-table-column label="类别名称" prop="title" sortable />
        <el-table-column label="审核" v-slot="scope" :width="110">
          <el-switch 
          v-model="scope.row.state"
          :active-value="2"
          :inactive-value="1"
          @change="handleSwitchChange(scope.row)"
          />
        </el-table-column>

        <el-table-column label="操作" :width="170" v-slot="scope">
          <el-button 
          size="mini"
          @click="edit(scope.row)"
          >修改</el-button>
          <el-button 
          type="danger" 
          size="mini"
          @click="handleRemove([scope.row.id])"
          >删除</el-button>
        </el-table-column>
      </el-table>

      <el-pagination
      class="gx-pagination"
      background
      :current-page.sync="condition.page"
      :page-size.sync="condition.pageSize"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[1, 2, 3]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="listInfo.totalpage" />
    </div>

  </div>
</template>

<script>
import {pick} from '@/utils'
export default {
  data(){
    return {
      visible: false,
      action: 'add',
      form: {},
      rules: {
        title: {required: true, message: '请输入分类名称'},
        thumb: {required: true, message: '请上传图片'},
      },

      condition: {
        //分页信息 pageSize 每页多少条
        page: 1,
        pageSize: 1,
      },
      listInfo: {
        result: [],
        totalpage: 0
      },

      //复选框选中的id值
      ids: [],

    }
  },
  watch: {
    condition: {
      handler(){
        this.query()
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    query(){
      this.$http.post('productcate/list', this.condition).then(res => {
        if(res.err > 0){
          this.$message.error(res.desc);
        }else{
          this.listInfo = res.info
        }
      })
    },
    handleSelectionChange( arr ){
      this.ids = arr.map(item => item.id);
    },
    handleCurrentChange( page ){
      this.condition.page = page;
    },
    handleSizeChange( size ){
      this.condition.pageSize = size;
    },
    handleSwitchChange({id, state}){
      this.$http.post('productcate/state', { id, state }).then(res => {
        if(res.err > 0){
          this.$message.error(res.desc);
        }else{
          this.$message.success(res.desc);
        }
      }) 
    },
    handleRemove( ids ){
      this.$confirm('您确定删除所选数据吗？', '提示').then(() => {
        this.$http.post('productcate/remove', {ids}).then(res => {
          if(res.err > 0){
            this.$message.error(res.desc);
          }else{
            this.query();
            this.$message.success(res.desc);
          }
        })
      }).catch(_ => this.$refs.table.clearSelection() )
    },
    add(){
      this.visible = true;
      this.action = 'add';
      this.form = { orderid: 1};
    },
    edit( row ){
      this.visible = true;
      this.action = 'edit';
      this.form = pick(row, ['id','title','thumb','orderid','state']);
    },
    save(){

      this.$refs.form.validate( vaild => {
        if( vaild ){
          
          this.$http.post(`productcate/${this.action}`, this.form).then(res => {
            if(res.err > 0){
              this.$message.error(res.desc);
            }else{
              this.query();
              this.$message.success(res.desc);
              this.visible = false;
            }
          })

        }
      })

    },
    beforeUpload (file) {
      const ext = file.name.match(/\.(\w+)$/i)[1].toLowerCase()
      const isPic = ['jpeg', 'png', 'gif', 'jpg', 'webp']
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isPic.includes(ext)) this.$message.error('上传图片只能是 ' + isPic + ' 格式!')
      if (!isLt2M) this.$message.error('上传图片大小不能超过 2MB!')
      return isPic && isLt2M
    },
    uploadSuccess (res) {
      if (res.err > 0) {
        this.$message.error(res.desc)
      } else {
        this.$message.success(res.desc)
        this.$set(this.form, 'thumb', res.result)
      }
    }
  }
}
</script>